package districting;

import java.util.Comparator;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;

public class PollingBoothCompactnessComparatorA implements
      Comparator<PollingBooth> {

   private double compact(Geometry geom) {
      return geom.getArea() / Math.pow(geom.getLength(), 2);
   }

   Division division;

   public PollingBoothCompactnessComparatorA(Division division) {
      this.division = division;
   }

   @Override
   public int compare(PollingBooth pb0, PollingBooth pb1) {
      Geometry geom = new GeometryFactory().createGeometry(division
            .getPolygon());
      Geometry newGeom0 = geom.union(pb0.getPolygon());
      Geometry newGeom1 = geom.union(pb1.getPolygon());
      
      if (newGeom0.getNumGeometries() < geom.getNumGeometries()) {
         return -1;
      } else if (newGeom1.getNumGeometries() < geom.getNumGeometries()) {
         return 1;
      }
      
      double compact0 = compact(newGeom0);
      double compact1 = compact(newGeom1);
      if (compact0 > compact1) {
         return -1;
      } else if (compact0 < compact1) {
         return 1;
      } else {
         return 0;
      }
   }

}
